package tictactoe.clientprocessors;

import tictactoe.Client;
import tictactoe.protocol.Protocol;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: Double
 * Date: 15.01.12
 * Time: 21:02
 * To change this template use File | Settings | File Templates.
 */
public class MultiClientProcessor implements Runnable,ClientProcessor
{
	private ArrayList<Client> clients = new ArrayList<Client>();
	private ArrayList<Client> preAuthClient = new ArrayList<Client>();
	private boolean flag = true;

	public MultiClientProcessor()
	{
		//new Thread(this).start();
	}
	public ArrayList<Client> getPreAuthClient()
	{
		return preAuthClient;
	}
	private void addPreAuthClient(Client client)
	{
		preAuthClient.add(client);
		System.out.println("preAuth client added");
	}
	@Override
	public void addClient(Client c)
	{
		addPreAuthClient(c);
	}
	private void checkValid(Client c)
	{
		if(isValid(c))
		{
			clients.add(c);
				c.sendMsg(Protocol.encode(Protocol.status, Protocol.authPassed));

		}else
		{
			try
			{
				c.sendMsg(Protocol.encode(Protocol.status, Protocol.authFail));
				c.disconnect();
			} catch (IOException e)
			{
				e.printStackTrace();
				Logger.getLogger("TicTacToeServer").log(Level.SEVERE,e.getMessage());
			}
		}
	}

	@Override
	public void removeClient(Client c)
	{
		clients.remove(c);
	}

	public boolean isValid(Client client)
	{
		boolean ret = true;
		if(clients.size()>0)
			for(Client c:clients)
			{
				if(client.getName().equalsIgnoreCase(c.getName()))
				{
					ret = false;
				}
			}
		System.out.println(client.getName() + "authorize state: "+ret);
		Logger.getLogger("TicTacToeServer").log(Level.SEVERE,client.getName() + "authorize state: "+ret);
		return ret;

	}

	@Override
	public ArrayList<Client> getClients()
	{
		return clients;  //To change body of implemented methods use File | Settings | File Templates.
	}

	@Override
	public void run()
	{
		System.out.println("MultiClientProcessor run() started");
		while(flag)
		{
			/*
			for(Client c:preAuthClient)
			{
				try
				{
					System.out.println("Now processing preAuthClient "+c);
					if(c.hasMessage())
					{
						System.out.println("PreAuth client "+c+" has message");
						Protocol.decodeSystemPacket(c.getMessage(),c,clients);
					}
				} catch (IOException e)
				{
					e.printStackTrace();
				}
				if(c.getName()!=null)
				{
					checkValid(c);
					preAuthClient.remove(c);
				}
			}   */

			for(int i=0;i<preAuthClient.size();i++)
			{


					if(preAuthClient.get(i).hasMessage())
					{
						System.out.println("PreAuth client "+preAuthClient.get(i)+" has message");
						Protocol.decodeSystemPacket(preAuthClient.get(i).getMessage(),preAuthClient.get(i),clients);
					}

				if(preAuthClient.get(i).getName()!=null)
				{
					checkValid(preAuthClient.get(i));
					preAuthClient.remove(i);
					System.out.println(preAuthClient.toArray());
				}
			}

			for(Client c:clients)
			{
				if(!c.isPlaying()){

					if(c.hasMessage())
					{
						Protocol.decodeSystemPacket(c.getMessage(),c,clients);
					}

				}
			}
		}
	}



	@Override
	public void getClientForGame(Client c)
	{

	}

	@Override
	public void returnClient(Client c)
	{
	}
}
